package com.dongdongshop.controller;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ConstantEnum;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LoginController {
    @RequestMapping(path={"/","index"})
    public String toIndex(){
        return "admin/index";
    }
    @RequestMapping("toLogin")
    public String toLogin(){
        return "/shoplogin";
    }
    @RequestMapping("login")
    @ResponseBody
    public Result login (String sellerId , String password){
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token =new UsernamePasswordToken(sellerId,password);
        try {
            subject.login(token);
        }catch (UnknownAccountException e){
            System.out.println("账号不存在");
            return Result.build(ConstantEnum.ERROR);
        }catch (IncorrectCredentialsException e){
            System.out.println("密碼錯誤");
            return Result.build(ConstantEnum.SUCCESS);
        }
        return Result.build(ConstantEnum.SUCCESS);
    }

    @RequestMapping("getIndex")
    public String getIndex(){
        return "/admin/index";
    }
}
